home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-09-05 | 2.1 KB | 72 lines |
- /**
- Company: Eyematic Interfaces
- Project: Shout3D 2.0 Sample Code
- Class: SpriteEffect
- Date: March 23, 2000
- Description: A simple example of a PostRenderEffect that displays a sprite over the scene
- (C) Copyright Eyematic Interfaces, Inc. - 1997-2000 - All rights reserved
- */
-
- package custom_nodes;
-
- import shout3d.core.*;
- import shout3d.*;
- import java.awt.*;
- import java.applet.*;
- import java.net.*;
-
- /**
- * SpriteEffect
- *
- * @author Jim Stewartson
- * @author Paul Isaacs
- */
-
- public class SpriteEffect extends PostRenderEffect implements FieldObserver{
-
- public final StringField imageUrl = new StringField( this, "imageUrl", Field.ANY, "");
- final public IntField xPosition = new IntField( this, "xPosition", Field.NON_NEGATIVE_INT, 0);
- final public IntField yPosition = new IntField( this, "yPosition", Field.NON_NEGATIVE_INT, 0);
- final public BooleanField centered = new BooleanField( this, "centered", Field.ANY, false);
-
-
- /**
- * Constructs a default SpriteEffect node.
- * NOTE: imageUrl is relative to directory in which model file is contained.
- */
- public SpriteEffect(){
- imageUrl.addFieldObserver(this, null);
- }
-
- public void onFieldChange(Field f, Object userData){
- if (f == imageUrl) getImage();
- }
-
- Image image;
- protected void getImage(){
- if (getViewer().getComponent() instanceof Applet){
- try{
- // getCurrentBaseURL() returns the directory in which the model file is located.
- URL myURL = new URL(getViewer().getResourceListener().getCurrentBaseURL(), imageUrl.getValue());
- Applet applet = (Applet)getViewer().getComponent();
- image = applet.getImage(myURL);
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- }
-
- public void filter(Graphics offScreenGraphics, int surface_pixel_bits[], float z_buffer[], int deviceWidth, int deviceHeight){
- if (image != null){
- int x = xPosition.getValue();
- int y = yPosition.getValue();
- if (centered.getValue()){
- x -= image.getWidth(null)/2;
- y -= image.getHeight(null)/2;
- }
- offScreenGraphics.drawImage(image, x, y, null);
- }
- }
-
- }